﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;

namespace TranTekBE.KPI_NhanSu
{
    public partial class Danh_Gia_Nguoi_Dung : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserInfo.MemberKPI != true)
            {
                //Load JS
                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
                if (Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null && Request.QueryString["admin"] == null)
                {
                    LoadData("", "", "", "");
                }
                else
                {
                    LoadData(Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"], Request.QueryString["admin"]);
                }
                if (!IsPostBack)
                {
                    // Get user
                    FunctionsUtils.GetDropListUser(droplist_ten_nhan_vien_loc_kq);
                    // Get stores
                    FunctionsUtils.GetDropListStore(droplist_cua_hang_loc_kq);
                    //
                    droplist_ten_nhan_vien_loc_kq.SelectedValue = Request.QueryString["user"];
                    droplist_cua_hang_loc_kq.SelectedValue = Request.QueryString["store"];
                    
                    txt_thoi_gian_loc.Text = Request.QueryString["time"];
                    droplist_PhanQuyen.SelectedValue = Request.QueryString["admin"];
                    //
                }
            }
            else
            {
                Response.Redirect("../Admin/DontPermission.aspx");
            }
            
        }
        // Get Avatar
        public string GetImage(object userId)
        {
            string Avatar = "";
            string fileName = userId + ".png";
            string path_image = Server.MapPath("~/images/KPI/UserImage/") + fileName;
            //
            if (System.IO.File.Exists(path_image))
            {
                Avatar = "../images/KPI/UserImage/" + fileName;
            }
            else
            {
                Avatar = "../images/KPI/UserImage/avatar_default.png";
            }
            return Avatar;
        }
        void LoadData(string getuser, string getstore, string gettime, string getadmin)
        {
            string sql_loc_user = @"SELECT *
            FROM tbl_UserKPI 
            WHERE
            ID_USER not in ('" + UserInfo.UserID + @"') and
            PHAN_QUYEN in ('user','manager')";
            string sql_user = "";
            string sql_stores = "";
            
            string sql_date = "";
            string sql_nhom = "";
            if (getuser != "" && getuser != null)
            {
                sql_user = " AND ID_USER = '" + getuser + "'";
            }

            if (getstore != "" && getstore != null)
            {
                sql_stores = " AND CUA_HANG = '" + getstore + "'";
            }
            
            if (gettime != "" && gettime != null)
            {
                sql_date = " AND NGAY_VAO_LAM = '" + gettime + "'";
            }
            if (getadmin != "" && getadmin != null && getadmin!="trong")
            {
                sql_nhom = " AND PHAN_QUYEN = '" + getadmin + "'";
            }

            sql_loc_user = sql_loc_user + sql_user + sql_stores  + sql_date + sql_nhom;
            //
            DataSet all_user_schedule = FunctionsUtils.Database2.ExecuteQuery(sql_loc_user);

            DataTable dt = all_user_schedule.Tables[0];

            PagedDataSource pgitems = new PagedDataSource();

            System.Data.DataView dv = new System.Data.DataView(dt);

            pgitems.DataSource = dv;

            pgitems.AllowPaging = true;

            pgitems.PageSize = 10;

            pgitems.CurrentPageIndex = PageNumber;

            if (pgitems.PageCount > 1)
            {

                rptPages.Visible = true;

                System.Collections.ArrayList pages = new System.Collections.ArrayList();

                for (int i = 0; i < pgitems.PageCount; i++)

                    pages.Add((i + 1).ToString());

                rptPages.DataSource = pages;

                rptPages.DataBind();

            }

            else

                rptPages.Visible = false;

            rpDanhGia.DataSource = pgitems;

            rpDanhGia.DataBind();

        }

        public int PageNumber
        {

            get
            {

                if (ViewState["PageNumber"] != null)

                    return Convert.ToInt32(ViewState["PageNumber"]);

                else

                    return 0;

            }

            set
            {

                ViewState["PageNumber"] = value;

            }

        }

        protected void rptPages_ItemCommand1(object source, RepeaterCommandEventArgs e)
        {

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            if (Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null && Request.QueryString["admin"] == null)
            {
                LoadData("", "", "","");
            }
            else
            {
                LoadData(Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"], Request.QueryString["admin"]);
            }

        }
        
        protected void btnLoc_Click(object sender, EventArgs e)
        {

            if (Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null && Request.QueryString["admin"] == null)
            {
                LoadData("", "", "", "");
            }
            else
            {
                LoadData(Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"], Request.QueryString["admin"]);
            }
            
        }

        protected void droplist_ten_nhan_vien_loc_kq_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Danh-Gia-Nguoi-Dung.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&admin=" + droplist_PhanQuyen.SelectedValue.ToString() + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString());
        }

        protected void droplist_cua_hang_loc_kq_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Danh-Gia-Nguoi-Dung.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&admin=" + droplist_PhanQuyen.SelectedValue.ToString() + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString());
        }

        protected void droplist_cua_hang_dang_lam_loc_kq_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Danh-Gia-Nguoi-Dung.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&admin=" + droplist_PhanQuyen.SelectedValue.ToString() + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString());
        }

        protected void txt_thoi_gian_loc_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("Danh-Gia-Nguoi-Dung.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&admin=" + droplist_PhanQuyen.SelectedValue.ToString() + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString());
        }

        protected void droplist_PhanQuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Danh-Gia-Nguoi-Dung.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&admin=" + droplist_PhanQuyen.SelectedValue.ToString() + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString());
        }
    }
}